package com.esd.download;

import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class test2 {
	public static void main(String[] args) throws InterruptedException {

		long l = System.currentTimeMillis();
		// TODO Auto-generated method stub
		// http://b2c.csair.com/B2C40/checkLogin/jaxb/checkLogin.ao
		WebClient webClient = new WebClient(BrowserVersion.CHROME);// 设置浏览器的User-Agent
		webClient.setJavaScriptTimeout(10000);// 设置JS执行的超时时间
		webClient.getOptions().setThrowExceptionOnScriptError(false);// 当JS执行出错的时候是否抛出异常
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);// 当HTTP的状态非200时是否抛出异常
		webClient.getOptions().setTimeout(30000);// 设置“浏览器”的请求超时时间
		webClient.getOptions().setCssEnabled(false);// 是否启用CSS
		webClient.getOptions().setJavaScriptEnabled(true); // 很重要，启用JS
		webClient.waitForBackgroundJavaScript(30000);// 设置JS后台等待执行时间
		webClient.setAjaxController(new NicelyResynchronizingAjaxController());// 很重要，设置支持AJAX
		System.out.println(System.currentTimeMillis() - l);

		try {
			String url="http://spjc.bjmtg.gov.cn/hrapp/ProcessType/ProcessSortServlet?NextToDo=tdSort&Parm=E67BB630-DC17-11DF-B630-B2678DA151BA";
			WebWindow webWindos = null;
			WebWindow w0 = webClient.getWebWindows().get(0);
			webWindos = webClient.openWindow(new URL(url), "page", w0);
			Page page = webWindos.getEnclosedPage();
			WebResponse response = page.getWebResponse();
			System.out.println(response.getContentType());// application/xhtml+xml
			System.out.println(page.isHtmlPage());
			HtmlPage hp = (HtmlPage)page;
			//System.out.println(hp.asXml());
			List<?> list =  hp.getByXPath("//a");
			for (Iterator<?> iterator = list.iterator(); iterator.hasNext();) {
				HtmlAnchor anchor = (HtmlAnchor) iterator.next();
				
				System.out.println("=========================================================");
				String href=anchor.getHrefAttribute();
				System.out.println(href);
				//if(href.startsWith("javascript:")) {
				if(href.startsWith("javascript:showDetail333()")) {
					HtmlPage c=anchor.click();
					System.out.println("=========================================================");
					System.out.println(c.getUrl());
					//System.out.println(c.asXml());
					System.out.println("*********************************************************");
					return;
				}
				
				
				
			}
//			if (response.getContentType().equals("application/xhtml+xml")) {
//				Map<String, Object> xml = XmlExercise.xml2json(response.getContentAsString());
//				csairHtmlPage.setLS(CS.NONE);
//				return xml;
//			}
			
			
//			HtmlPage page = webClient.getPage("http://www.bjmtg.gov.cn/zwgk/qld/");
		
			
			
		} catch (FailingHttpStatusCodeException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
